//
// Created by gogoqiu on 2021/9/25.
//

#include "dh_bitmap_button.h"

/*
 * File:   DhBitmapButton.cpp
 * Author: Sai
 *
 * Created on 2009年12月29日, 下午4:08
 */

#include "dh_bitmap_button.h"

BEGIN_EVENT_TABLE(DhBitmapButton, wxControl)
                EVT_PAINT(DhBitmapButton::OnPaint)
                EVT_ENTER_WINDOW(DhBitmapButton::OnEnter)
                EVT_LEAVE_WINDOW(DhBitmapButton::OnLeave)
                EVT_LEFT_DOWN(DhBitmapButton::OnDown)
                EVT_LEFT_DCLICK(DhBitmapButton::OnDClick)
                EVT_LEFT_UP(DhBitmapButton::OnUp)
END_EVENT_TABLE()

IMPLEMENT_DYNAMIC_CLASS(DhBitmapButton, wxControl)

DhBitmapButton::DhBitmapButton()
{
}

DhBitmapButton::DhBitmapButton(wxWindow* parent, wxWindowID id,
                               const wxPoint& pos,
                               const wxSize& size,
                               long style,
                               const wxValidator& validator)
        : normal_bitmap_(NULL), down_bitmap_(NULL), enter_bitmap_(NULL)
{
    Create(parent, id, pos, size, style, validator);
}

DhBitmapButton::~DhBitmapButton()
{
    wxDELETE(normal_bitmap_);
    wxDELETE(enter_bitmap_);
    wxDELETE(down_bitmap_);
}

bool DhBitmapButton::Create(wxWindow* parent, wxWindowID id,
                            const wxPoint& pos,
                            const wxSize& size,
                            long style,
                            const wxValidator& validator)
{
    normal_bitmap_ = NULL;
    down_bitmap_ = NULL;
    enter_bitmap_ = NULL;
    disable_bitmap_ = NULL;

    if (!wxControl::Create(parent, id, pos, size, style, validator))
    {
        return false;
    }
    SetBackgroundStyle(wxBG_STYLE_PAINT);
    is_used_bg_ = false;
    return true;
}

wxSize DhBitmapButton::DoGetBestSize() const
{
    return GetSize();
}

void DhBitmapButton::DrawExistBitmap(wxDC* dc, wxBitmap* image1,
                                     wxBitmap* exist_image)
{
    if (image1)
    {
        dc->DrawBitmap(*image1, 0, 0, true);

    } else
    {
        dc->DrawBitmap(*exist_image, 0, 0, true);
    }
}

void DhBitmapButton::OnPaint(wxPaintEvent& event)
{
    wxPaintDC dc(this);
    DrawBackground(&dc);
    //1.状态控制绘画,好处就是可以调用Refresh连背景一起刷新.
    switch (button_status_)
    {
        case kDhBitmapButtonNormal:
            dc.DrawBitmap(*normal_bitmap_, 0, 0, true);
            break;
        case kDhBitmapButtonEnter:
            if (!enter_bitmap_)
            {
                int width = DoGetBestSize().GetWidth();
                int height = DoGetBestSize().GetHeight();
                wxClientDC dc(this);
                dc.SetPen(*wxRED_PEN);
                dc.SetBrush(*wxTRANSPARENT_BRUSH);

                dc.DrawRectangle(0, 0, width, height);
                break;
            }
            dc.DrawBitmap(*enter_bitmap_, 0, 0, true);
            break;
        case kDhBitmapButtonDown:
            DrawExistBitmap(&dc,down_bitmap_,normal_bitmap_);
            break;
        case kDhBitmapButtonUp:
            dc.DrawBitmap(*normal_bitmap_, 0, 0, true);
            break;
        case kDhBitmapButtonLeave:
            dc.DrawBitmap(*normal_bitmap_, 0, 0, true);
            break;
        case kDhBitmapButtonDClick:
            DrawExistBitmap(&dc,down_bitmap_,normal_bitmap_);
            break;
        case kDhBitmapButtonDisable:
            DrawExistBitmap(&dc,disable_bitmap_,normal_bitmap_);
            break;
        default:
            dc.DrawBitmap(*normal_bitmap_, 0, 0, true);
            break;
    }

}

void DhBitmapButton::DrawBackground(wxDC* dc)
{
    if (is_used_bg_)
    {
        dc->DrawBitmap(background_, 0, 0, true);
    } else
    {
        wxBrush brush(GetBackgroundColour());
        wxPen pen(GetBackgroundColour());
        dc->SetBrush(brush);
        dc->SetPen(pen);
        dc->DrawRectangle(0, 0, GetSize().x, GetSize().y);
    }
}

void DhBitmapButton::OnEnter(wxMouseEvent& event)
{
    button_status_ = kDhBitmapButtonEnter;
    Refresh();
    Update();
}

void DhBitmapButton::OnLeave(wxMouseEvent& event)
{
    if (!IsEnabled())
    {
        return;
    }
    button_status_ = kDhBitmapButtonLeave;
    Refresh();
    Update();
}

void DhBitmapButton::OnDClick(wxMouseEvent& event)
{
    button_status_ = kDhBitmapButtonDown;
    Refresh(false);
    Update();
}

void DhBitmapButton::OnDown(wxMouseEvent& event)
{
    button_status_ = kDhBitmapButtonDown;
    Refresh();
    Update();
}

void DhBitmapButton::OnUp(wxMouseEvent& event)
{
    if (kDhBitmapButtonDown != button_status_)
    {
        return;
    }
    button_status_ = kDhBitmapButtonUp;
    Refresh();
    Update();
    wxCommandEvent myEvent(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
    myEvent.SetEventObject(this);
    GetEventHandler()->ProcessEvent(myEvent);
}

DhBitmapButton* DhBitmapButton::set_normal_bitmap(wxBitmap* bitmap)
{
    normal_bitmap_ = bitmap;
    return this;
}

DhBitmapButton* DhBitmapButton::set_down_bitmap(wxBitmap* bitmap)
{
    down_bitmap_ = bitmap;
    return this;
}

DhBitmapButton* DhBitmapButton::set_enter_bitmap(wxBitmap* bitmap)
{
    enter_bitmap_ = bitmap;
    return this;
}

bool DhBitmapButton::Enable(bool enable)
{
    if (enable)
    {
        button_status_ = kDhBitmapButtonNormal;
    } else
    {
        button_status_ = kDhBitmapButtonDisable;
    }

    Refresh(false);
    Update();
    return wxControl::Enable(enable);
}

bool DhBitmapButton::Disable()
{
    return Enable(false);
}

DhBitmapButton* DhBitmapButton::set_disable_bitmap(wxBitmap* bitmap)
{
    disable_bitmap_ = bitmap;
    return this;
}

DhBitmapButton* DhBitmapButton::set_background(const wxBitmap& bitmap)
{
    is_used_bg_ = true;
    background_ = bitmap;
    return this;
}

bool DhBitmapButton::SetBackgroundColour(const wxColour& colour)
{
    is_used_bg_ = false;
    return wxControl::SetBackgroundColour(colour);
}